/**
 * 
 */
package com.geezdata.cps.ttk.query.query.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.geezdata.cps.base.enums.ErrorCode;
import com.geezdata.cps.base.exceptions.MyException;
import com.geezdata.cps.base.utils.BeanUtil;
import com.geezdata.cps.ttk.common.request.QueryTaskDetailRequest;
import com.geezdata.cps.ttk.common.request.QueryTaskListRequest;
import com.geezdata.cps.ttk.common.response.QueryTaskDetailResult;
import com.geezdata.cps.ttk.common.response.QueryTaskListResult;
import com.geezdata.cps.ttk.common.vo.PinduoduoDDKGoodsDetailResVO;
import com.geezdata.cps.ttk.common.vo.SharerResVO;
import com.geezdata.cps.ttk.common.vo.TaskResVO;
import com.geezdata.cps.ttk.domain.domain.Sharer;
import com.geezdata.cps.ttk.domain.domain.Task;
import com.geezdata.cps.ttk.integration.ddk.DdkIntegrationService;
import com.geezdata.cps.ttk.mapper.mapper.TaskMapper;
import com.geezdata.cps.ttk.query.query.SharerService;
import com.geezdata.cps.ttk.query.query.TaskItemService;
import com.geezdata.cps.ttk.query.query.TaskService;
import com.google.common.collect.Maps;

/**
 * @author sszheng
 * @date 创建时间：2018年10月26日 下午6:00:41
 *
 */
@Service
public class TaskServiceImpl implements TaskService {
	@Autowired
	private TaskMapper taskMapper;
	@Autowired
	private SharerService sharerService;
	@Autowired
	private DdkIntegrationService ddkIntegrationService;
	@Autowired
	private TaskItemService taskItemService;

	@Override
	public Task getDetail(Integer taskId) {
		return taskMapper.selectByPrimaryKey(taskId);
	}

	@Override
	public QueryTaskDetailResult getTaskDetail(QueryTaskDetailRequest request) {
		QueryTaskDetailResult result = new QueryTaskDetailResult();
		
		Task task = taskMapper.selectByPrimaryKey(request.getTaskId());
		if(task.getStatus() == 0) {
			throw new MyException(ErrorCode.TASK_CANCEL);
		}
		
		TaskResVO taskResVO = BeanUtil.convert(task, TaskResVO.class);
		
		Sharer sharer = sharerService.getSharerByOpenId(task.getOpenId());
		SharerResVO sharerVO = BeanUtil.convert(sharer, SharerResVO.class);
		
		PinduoduoDDKGoodsDetailResVO goods = ddkIntegrationService.getDDKGoodsDetail(task.getGoodsId());
		
		taskResVO.setHeader(sharerVO);
		taskResVO.setGoods(goods);
		
		Integer canBuy = getCanBuy(task, request.getOpenId());
		result.setCanBuy(canBuy);
		
		result.setTask(taskResVO);
		return result;
	}
	
	private Integer getCanBuy(Task task, String openId) {
		Integer maxPurchaseNum = getMaxPurchaseNum(task.getTaskId()); 
		Integer stn = taskItemService.getExistsTaskItemNum(task.getTaskId(), openId);
		return stn >= maxPurchaseNum? 0: 1;
	}

	@Override
	public QueryTaskListResult getTaskList(QueryTaskListRequest request) {
		QueryTaskListResult result = new QueryTaskListResult();
		Map<String, Object> params = Maps.newHashMap();
		params.put("openId", request.getOpenId());
		params.put("status", 1);
		List<Task> taskList = taskMapper.selectBatch(params);
		if(CollectionUtils.isEmpty(taskList)) {
			return result;
		}
		
		List<TaskResVO> taskResVOList = BeanUtil.convertList(taskList, TaskResVO.class);
		taskResVOList.stream().forEach(taskResVO -> setExt(taskResVO));
		result.setList(taskResVOList);
		return result;
	}

	private void setExt(TaskResVO taskResVO) {
		PinduoduoDDKGoodsDetailResVO goods = ddkIntegrationService.getDDKGoodsDetail(taskResVO.getGoodsId());
		taskResVO.setGoods(goods);
	}
	
	@Override
	public Integer getMaxPurchaseNum(Integer taskId) {
		Task task = taskMapper.selectByPrimaryKey(taskId);
		return task.getMaxPurchaseNum();
	}
}
